﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Mock1G3.BussinessLogic;
using Mock1G3.BaseEntities;
using System.Data;
using System.Threading;

namespace Mock1G3.UserControl.UCTransferCash
{
    public partial class ReceiveAccount : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadAccountInfo();

            }
            lblMessage.Visible = false;
        }

        public void btnAcceptAccRNo_Click(object sender, EventArgs e)
        {
            switch ((sender as Button).ID)
            {
                case "btnAcceptAccReceiveId":
                    LoadAccountReceiveInfo();
                    break;
                case "btnAcceptReceiveAccount":
                    DisableControl();
                    lblNotice.Text = "Transfer Cash Amount";
                    txtAmount.Visible = true;
                    lblAmount.Visible = true;
                    lblAmount.Text = "Amount:";
                    btnAcceptReceiveAccount.Visible = false;
                    btnAcceptAmount.Visible = true;
                    break;
                case "btnAcceptAmount":
                    LoadAccountReceiveWithAmount();
                    break;
                case "btnAcceptTransfer":
                    TransferCash();
                    break;
                case "btnPrintReceipt":
                    ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "alert", "alert('Print success!');redirectHome();", true);
                    Session.Remove("AccountNo");
                    Session.Remove("AccountReceiveNo");
                    break;
                case "btnReject":
                    Session.RemoveAll();
                    Response.Redirect("Default.aspx");
                    break;
            }
        }

        public void TransferCash()
        {
            Account account = new Account();
            TransferCashLogic transferLogic = new TransferCashLogic();
            Log log = new Log();
            account.AccountNo = Session["AccountNo"].ToString();
            account.AccountNoRev = Session["AccountReceiveNo"].ToString();
            decimal amount = decimal.Parse(lblAmountValue.Text);
            if (transferLogic.TransferCash(account, amount))
            {
                Thread.Sleep(TimeSpan.FromSeconds(3));
                log.ATMID = int.Parse(Session["ATMID"].ToString());
                log.CardNo = Session["CardNo"].ToString();
                log.LogDate = DateTime.Now;
                log.Amount = amount;
                log.Detail = "Transfer Cash into other account";
                transferLogic.WriteLog(log);

                lblMessage.Visible = false;
                DisableControl();
                lblNotice.Text = "Transfer Success!";
                lblText.Visible = true;
                lblText.Text = "Do you want to get receipt?";
                btnAcceptTransfer.Visible = false;
                btnPrintReceipt.Visible = true;
                ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "alert", "alert('Transfer Success! Thank you for using our service!');", true);
            }
            else
            {
                lblMessage.Text = "Transfer Error\nPlease perform the transaction again!";
            }
        }

        public void LoadAccountReceiveWithAmount()
        {
            TransferCashLogic transferLogic = new TransferCashLogic();
            AccountTransferLogic accountLogic = new AccountTransferLogic();
            Account account = new Account();
            account.AccountNo = Session["AccountNo"].ToString();
            if (txtAmount.Text == "")
            {
                lblMessage.Visible = true;
                lblMessage.Text = "* You have not entered Amount!";
            }
            else if (!transferLogic.IsNumber(txtAmount.Text))
            {
                lblMessage.Visible = true;
                lblMessage.Text = "* Amount can not be character!";
            }
            else if (!transferLogic.CheckTransferLimit(txtAmount.Text))
            {
                lblMessage.Visible = true;
                lblMessage.Text = "* Range of Amount between 0 and 30.000000!";
            }
            else if (!transferLogic.CheckBalanceAmount(account, txtAmount.Text))
            {
                Thread.Sleep(TimeSpan.FromSeconds(2));
                lblMessage.Visible = true;
                lblMessage.Text = "* Your account is not enough to transfer!";
            }
            else
            {
                Thread.Sleep(TimeSpan.FromSeconds(2));
                account.AccountNoRev = Session["AccountReceiveNo"].ToString();
                DataTable dt = accountLogic.GetAccountReceive(account);
                DataRow dr = dt.Rows[0];
                lblAccNo.Visible = true;
                lblAccName.Visible = true;
                lblAccNameValue.Visible = true;
                lblAccNoValue.Visible = true;
                lblAmountValue.Visible = true;
                txtAmount.Visible = false;
                lblMessage.Visible = false;

                lblNotice.Text = "Transfer Cash Information";
                lblAccNo.Text = "Receive Account No:";
                lblAccName.Text = "Receive Account Name:";
                lblAmount.Text = "Amount Transfer:";
                lblAccNameValue.Text = dr["Name"].ToString();
                lblAccNoValue.Text = account.AccountNoRev;
                lblAmountValue.Text = string.Format("{0:0,0}", txtAmount.Text);

                btnAcceptAmount.Visible = false;
                btnAcceptTransfer.Visible = true;
            }
        }

        public void LoadAccountReceiveInfo()
        {
            AccountTransferLogic accountLogic = new AccountTransferLogic();
            if (txtAccReceiveID.Text == "")
            {
                lblMessage.Visible = true;
                lblMessage.Text = "* You have to enter your Receive Account Number!";
            }
            else if (!accountLogic.IsNumber(txtAccReceiveID.Text))
            {
                lblMessage.Visible = true;
                lblMessage.Text = "* Account Receive Number can not contain character!";
            }
            else
            {
                Thread.Sleep(TimeSpan.FromSeconds(2));
                Account account = new Account();
                DataTable dt = new DataTable();
                account.AccountNoRev = txtAccReceiveID.Text;
                if (accountLogic.CheckAccountReceive(account))
                {
                    Session.Add("AccountReceiveNo", account.AccountNoRev);
                    dt = accountLogic.GetAccountReceive(account);
                    DataRow dr = dt.Rows[0];
                    lblNotice.Text = "Receive Information";
                    lblAccName.Text = "Account Receive Name:";
                    lblAccNo.Text = "Account Receive No:";
                    lblAccNameValue.Text = dr["Name"].ToString();
                    lblAccNoValue.Text = dr["AccountNo"].ToString();

                    txtAccReceiveID.Visible = false;
                    lblAccReceiveID.Visible = false;
                    btnAcceptReceiveAccount.Visible = true;
                    btnAcceptAccReceiveId.Visible = false;
                }
                else
                {
                    lblMessage.Visible = true;
                    lblMessage.Text = "* Account Receive Number not exist!";
                }

            }
        }

        public void LoadAccountInfo()
        {
            AccountTransferLogic accountLogic = new AccountTransferLogic();
            Account account = new Account();
            account.AccountNo = Session["AccountNo"].ToString();
            DataTable dt = new DataTable();
            dt = accountLogic.GetAccountSend(account);
            DataRow dr = dt.Rows[0];

            lblNotice.Text = "Please enter account number want to transfer";
            lblAccName.Text = "Account Transfer Name:";
            lblAccNo.Text = "Account Transfer No:";
            lblAccReceiveID.Text = "Account Receive No: ";
            lblHelp.Text = "<i>Press Accept to submit, Reject to quit!</i>";

            lblAccNameValue.Text = dr["Name"].ToString();
            lblAccNoValue.Text = dr["AccountNo"].ToString();
        }

        public void DisableControl()
        {
            lblAccName.Visible = false;
            lblAccNo.Visible = false;
            lblAccReceiveID.Visible = false;
            lblAmount.Visible = false;

            lblAccNameValue.Visible = false;
            lblAccNoValue.Visible = false;
            lblAmountValue.Visible = false;
            txtAccReceiveID.Text = string.Empty;
        }

        public void EnableControl()
        {
            lblAccName.Visible = false;
            lblAccNo.Visible = false;
            lblAccReceiveID.Visible = false;

            lblAccNameValue.Visible = false;
            lblAccNoValue.Visible = false;
        }
    }
}